package com.linq.cool.beauty.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linq.cool.api.domain.BeautyMember;
import com.linq.cool.beauty.domain.*;
import com.linq.cool.beauty.dto.BeautyOrderEvaluationDTO;
import com.linq.cool.beauty.dto.BeautyOrderEvaluationQueryDTO;
import com.linq.cool.beauty.enums.OrderStateEnum;
import com.linq.cool.beauty.mapper.BeautyOrderEvaluationMapper;
import com.linq.cool.beauty.service.*;
import com.linq.cool.common.constants.Constants;
import com.linq.cool.common.constants.UserConstants;
import com.linq.cool.common.core.utils.BeanUtils;
import com.linq.cool.common.core.utils.IdWorker;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.core.utils.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author: yqlin
 * @Date: 2021/2/15 22:39
 * @Description:
 * @Version: 1.0.0
 */

@Service
public class BeautyOrderEvaluationServiceImpl extends ServiceImpl<BeautyOrderEvaluationMapper, BeautyOrderEvaluation> implements BeautyOrderEvaluationService {
    @Autowired
    private BeautyMemberService memberService;
    @Autowired
    private BeautyOrderItemService orderItemService;
    @Autowired
    private BeautyOrderService orderService;
    @Autowired
    private BeautyOrderHistoryService orderHistoryService;
    @Autowired
    private BeautyProductStatisticalService productStatisticalService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveMemberOrderEvaluation(BeautyOrderEvaluationDTO dto) {
        // 查询用户信息
        BeautyMember member = memberService.getById(SecurityUtils.getUserId());
        //  将dto转换
        BeautyOrderEvaluation orderComment = new BeautyOrderEvaluation();
        BeanUtils.copyProperties(dto, orderComment);
        orderComment.setId(SpringUtils.getBean(IdWorker.class).nextId())
                .setMemberAvatar(member.getAvatar())
                .setMemberId(member.getId())
                .setMemberNickname(member.getNickname())
                .setType(UserConstants.StateEnum.COMMENT_SIMPLE.getCode());
        // 从订单项中拿出商品信息
        BeautyOrderItem orderItem = orderItemService.getById(dto.getOrderItemId());
        // 更新orderItem状态
        orderItem.setIsEvaluation(UserConstants.StateEnum.ORDER_EVALUATION_STATUS_1.getCode())
                .setEvaluationTime(new Date())
                .setGmtModified(new Date());
        orderItemService.updateById(orderItem);
        orderComment.setProductId(orderItem.getProductId()).setProductName(orderItem.getProductName());
        // 保存评价
        this.save(orderComment);
        // 订单状态改为已评价，保存订单操作历史  只有前面状态是待评价的才能操作
        BeautyOrder order = orderService.getById(dto.getOrderId());
        if (OrderStateEnum.ALREADY_SIGN.getCode().equals(order.getStatus())) {
            order.setStatus(OrderStateEnum.FINISH.getCode())
                    .setIsEvaluation(UserConstants.StateEnum.ORDER_EVALUATION_STATUS_1.getCode())
                    .setEvaluationTime(new Date())
                    .setGmtModified(new Date());
            orderService.updateById(order);
            // 存储订单历史表信息
            BeautyOrderHistory orderHistory = new BeautyOrderHistory();
            orderHistory.setId(SpringUtils.getBean(IdWorker.class).nextId())
                    .setMemberId(member.getId())
                    .setMemberPhone(member.getPhone())
                    .setMemberRealName(member.getRealName())
                    .setOrderId(order.getId())
                    .setRemark(order.getRemark())
                    // 代付款
                    .setOrderStatus(OrderStateEnum.FINISH.getCode());
            orderHistoryService.save(orderHistory);
        }
        // 统计下单数 更新
        BeautyProductStatistical one = productStatisticalService.getOne(new LambdaQueryWrapper<BeautyProductStatistical>()
                                                                                .eq(BeautyProductStatistical::getProductId, orderItem.getProductId())
        );
        if (null == one) {
            productStatisticalService.save(new BeautyProductStatistical().setProductId(orderItem.getProductId()).setOrderCount(1));
        } else {
            one.setOrderCount(one.getOrderCount() + 1);
            productStatisticalService.updateById(one);
        }
    }

    @Override
    public IPage<BeautyOrderEvaluation> listByPage(IPage<BeautyOrderEvaluation> page, BeautyOrderEvaluationQueryDTO dto) {
        return this.page(page, this.getLambdaQueryWrapper(dto));
    }

    @Override
    public List<BeautyOrderEvaluation> listProductEvaluations(Long productId) {
        return baseMapper.listProductEvaluations(productId);
    }

    private LambdaQueryWrapper<BeautyOrderEvaluation> getLambdaQueryWrapper(BeautyOrderEvaluationQueryDTO dto) {
        return new LambdaQueryWrapper<BeautyOrderEvaluation>()
                .eq(StringUtils.checkValNotNull(dto.getOrderId()), BeautyOrderEvaluation::getOrderId, dto.getOrderId())
                .eq(StringUtils.checkValNotNull(dto.getProductId()), BeautyOrderEvaluation::getProductId, dto.getProductId())
                .like(StringUtils.checkValNotNull(dto.getProductName()), BeautyOrderEvaluation::getProductName, dto.getProductName())
                .like(StringUtils.checkValNotNull(dto.getMemberNickname()), BeautyOrderEvaluation::getMemberNickname, dto.getMemberNickname())
                .eq(StringUtils.checkValNotNull(dto.getStar()), BeautyOrderEvaluation::getStar, dto.getStar())
                .orderByDesc(BeautyOrderEvaluation::getGmtCreate)
                .between(StringUtils.checkValNotNull(dto.getParams().get(Constants.BEGIN_TIME)), BeautyOrderEvaluation::getGmtCreate, dto.getParams().get(Constants.BEGIN_TIME), dto.getParams().get(Constants.END_TIME));
    }
}
